package cn.wowjoy.pda.android_pharmacy.mvvm.presenter;

import android.content.Intent;
import android.util.Log;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

import cn.bingoogolapple.androidcommon.adapter.BGABindingViewHolder;
import cn.wowjoy.pda.android_pharmacy.api.ApiService;
import cn.wowjoy.pda.android_pharmacy.common.core.BasePresenter;
import cn.wowjoy.pda.android_pharmacy.common.core.http.ServiceFactory;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.common.utils.DataCompare;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MyToast;
import cn.wowjoy.pda.android_pharmacy.model.xml.XmlUtils;
import cn.wowjoy.pda.android_pharmacy.model.xmlBaseResponse.RoleInfoResponseEnvelope;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.inHospitalPatientInfo.InHospitalPillNode;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.inHospitalPatientInfo.InHospitalPillTotalBeen;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.InHospitalPillIndexViewModel;
import cn.wowjoy.pda.android_pharmacy.ui.activity.hospital.pill.InHospitalPillMedicineDetailActivity;
import gear.yc.com.gearlibrary.rxjava.rxbus.RxBus;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/8/31.
 */

public class InHospitalPillIndexPresenter extends BasePresenter<InHospitalPillIndexViewModel> {
    private ApiService mApiService;
    private MDialog waitDialog;

    @Override
    public void onCreatePresenter() {
        mApiService = (ApiService) ServiceFactory.getInstance().getService(ApiService.class);
        viewModel.wlinnerAdapter.setItemEventHandler(this);
    }
    public void onTestItemClickItem(BGABindingViewHolder holder, InHospitalPillTotalBeen model) {
        //TODO:跳转传递数据
        Intent mIntent = new Intent(activity, InHospitalPillMedicineDetailActivity.class);
        mIntent.putExtra("DJJLID", model.getDJJLID());
        mIntent.putExtra("DJBQMC", model.getDJBQMC());
        activity.startActivity(mIntent);
    }
    public  void getDataList(){
        //联网访问接口  获取所有待处理上架订单
        Disposable disposable = null;
        disposable = mApiService.doXml(XmlUtils.prepare(InHospitalPillNode.getTotalContent("PDAQ10")))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {
                    @Override
                    protected void onStart() {
                        if(null == waitDialog){
                            waitDialog = CreateDialog.waitingDialog(activity);
                        }
                    }

                    @Override
                    public void onNext(RoleInfoResponseEnvelope xmlData) {
                        CreateDialog.dismiss(activity);
//                         waitDialog.dismiss();
                        //TODO:门诊信息获取失败 不跳转并提示重新扫码
                        if(!"0".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)){
                            new MyToast(activity).showinfo("上架单信息获取失败");
                            return;
                        }
                        List<InHospitalPillTotalBeen> inHospitalPillTotalBeens = XmlUtils.getInHospitalPillBeenList(xmlData.body.roleInfoResponse.retXml);

                        if(null != inHospitalPillTotalBeens && inHospitalPillTotalBeens.size()!=0){
                            RxBus.getInstance().post(Contants.INHOSPITAL_PILL_INDEX_PENDING_COUNT, inHospitalPillTotalBeens);
                            if(null == inHospitalPillTotalBeens.get(0).getDJJLID() || inHospitalPillTotalBeens.get(0).getDJJLID().equals("")){
                                inHospitalPillTotalBeens.get(0).setShow(false);
                            }
                            Collections.sort(inHospitalPillTotalBeens,new DataCompare());
                            viewModel.setData(inHospitalPillTotalBeens);
                        }
                    }
                    @Override
                    public void onComplete() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        CreateDialog.dismiss(activity);
//                         waitDialog.dismiss();
                        if(null != e && null != e.getMessage()){
                            Log.e("Throwable", e.getMessage());
                            new MyToast(activity).showinfo(e.getMessage());
                        }

                    }
                });
        addDisposable(disposable);
    }
    public void loadData(final String DJJLID) {
        //TODO:联网获取数据
        Disposable disposable = null;
        disposable = mApiService.doXml(XmlUtils.prepare(InHospitalPillNode.getTotalContent("PDAQ10")))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {
                    @Override
                    protected void onStart() {
                        if(null == waitDialog){
                            waitDialog = CreateDialog.waitingDialog(activity);
                        }
                    }

                    @Override
                    public void onNext(RoleInfoResponseEnvelope xmlData) {
                        CreateDialog.dismiss(activity);
                        //TODO:住院信息获取失败 不跳转并提示重新扫码
                        if (!"0".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)) {
                            new MyToast(activity).showinfo("信息获取失败，请重新扫码");
                            return;
                        }
                        //TODO:住院信息获取正确 跳转并传值
                        List<InHospitalPillTotalBeen> inHospitalPillTotalBeens = XmlUtils.getInHospitalPillBeenList(xmlData.body.roleInfoResponse.retXml);
                        Intent intent = new Intent(activity, InHospitalPillMedicineDetailActivity.class);
                        intent.putExtra("InHospitalPillBeenList", (Serializable) inHospitalPillTotalBeens);
                        intent.putExtra("DJJLID", DJJLID);
                        activity.startActivity(intent);
                    }

                    @Override
                    public void onComplete() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        CreateDialog.dismiss(activity);
                        if(null != e && null != e.getMessage()){
                            Log.e("Throwable", e.getMessage());
                            new MyToast(activity).showinfo(e.getMessage());
                        }
                    }
                });
        addDisposable(disposable);
    }


}
